import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/Login"
import ErrorPage from "@/views/404"
import Layout from "@/layout"

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


Vue.use(VueRouter)
export const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login,
    hidden: true,
  },
  {
    path: '/404',
    name: '404',
    component: ErrorPage,
    hidden: true,
  },
  {
    path: "/",
    name: "home",
    component: Layout,
    redirect: "/dashbord",
    children: [
      {
        path: "/dashbord",
        name: "Dashbord",
        component: () => import("@/views/dashbord"),
        meta: { title: "首页", icon: "el-icon-eleme" }
      }
    ]
  },
  {
    path: "/",
    name: "hope",
    component: Layout,
    redirect: "/hope",
    children: [
      {
        path: "/hope",
        name: "hope",
        component: () => import("@/views/hope"),
        meta: { title: "希望", icon: "el-icon-eleme" }
      }
    ]
  },
  {
    path: "*",
    name: "404",
    component: ErrorPage
  }
]

const createRouter = () => new VueRouter({
  scrollBehavior: () => ({ y: 0 }),
  routes
})

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

const router = createRouter()

export default router
